#include "CPath.h"
#include "crc32.h"

//----------------------------------------------
void CPath::operator= (const xchar* sStr)
{
	xchar* p = const_cast<xchar*>(sStr);
	m_uiNb = 0;
	while (*p != 0)
	{
		m_sStr[m_uiNb] = *p;
		p++;
		m_uiNb++;
		if (m_uiNb == MAX_PATH_CHARS-1)
			break;
	}
	m_sStr[m_uiNb] = 0;

	// Hash padding security
	m_sStr[m_uiNb+1] = 0;
	m_sStr[m_uiNb+2] = 0;
	m_sStr[m_uiNb+3] = 0;

	normalize();

	computeHash();
}

//----------------------------------------------
void CPath::computeHash()
{
	int iSize		= m_uiNb * sizeof(xchar);
	iSize			+= (4 - iSize % 4) % 4;
	m_uiHash = computeCrc32(reinterpret_cast<uchar*>(m_sStr), iSize);
}

//----------------------------------------------
void CPath::normalize()
{
	for (size_t i=0; i<m_uiNb; i++)
	{
		if (m_sStr[i] == '\\')
			m_sStr[i] = '/';
	}
}